#include "screencapsaver.h"
#include "core/controller.h"

ScreencapSaver::ScreencapSaver(QObject *parent) : QObject(parent)
{

}

bool ScreencapSaver::diskSpaceDetection(QString savePath, bool &diskSpaceLow, bool &fileExist)
{
    QFileInfo fileInfo(savePath);
    if( !fileInfo.exists() ){
        qDebug() << "ScreencapSaver::DiskSpaceDetection() 保存路径不存在" << savePath;
        fileExist = false;
        return false;
    }

    QStorageInfo storageInfo(fileInfo.absoluteFilePath());
    if (!storageInfo.isValid()) {
        qDebug() << "ScreencapSaver::DiskSpaceDetection() 存储信息无效";
        return false;
    }

    qint64 totalSize = storageInfo.bytesTotal() / (1024 * 1024) ;   //总空间
    double freeSize = static_cast<double>(storageInfo.bytesAvailable()) / (1024 * 1024); //剩余空间

    if(freeSize <= 100){
        qDebug()<<"ScreencapSaver::DiskSpaceDetection() 存储空间小于100M";
        diskSpaceLow = true;
        Controller::getInstance()->diskSpaceLow();

        return true;
    }
    return false;
}

bool ScreencapSaver::saveInfoNotification(QString completePath, bool diskSpaceLow)
{
    QString title, message, text;
    QFile file(completePath);
    bool result = false;

    if(!file.exists() && diskSpaceLow){ //录制前检测存储空间不足
        qDebug()<<"ScreencapSaver::saveInfoNotification 存储空间不足 "<< completePath ;

        title = tr("The storage path has less than 100MB of space available,the screen recording has ended!");
        text = "";

    }else if(file.exists() && file.size() > 0){
        qDebug()<<"ScreencapSaver::saveInfoNotification 保存到文件成功 "<<completePath ;

        QFileInfo fileInfo(completePath);
        QString savePath = fileInfo.absolutePath();
        title = tr("The screen recording has been saved successfully!");
        if(diskSpaceLow){
            title = tr("The storage path has less than 100MB of space available,the screen recording has ended!");
        }
        message = tr("successfully saved to") + savePath + tr(",Click to open the save video>>");
        text = "<a href=\"" + completePath + "\">" + message + "</a>";

        result = true;

    }else{
        qDebug()<<"ScreencapSaver::saveInfoNotification 保存到文件失败 "<<completePath;

        title = tr("The screen recording failed to save!");
        text = "";

    }
    SystemNotification().sendMessage(title, text);
    //保存成功执行完成后的动作
    if(result){
        completionAction(ConfigHandler().getCompletionAction(),completePath);
    }
    return result;
}

void ScreencapSaver::completionAction(SaveAction completionAction, QString completeSavePath)
{
    if(completionAction == SaveAction::ONLY_SAVE){
        return;
    }

    if(completionAction == SaveAction::OPEN_FILE){
        qDebug()<<"自动播放";
        QDesktopServices::openUrl(QUrl::fromLocalFile(completeSavePath));
        return;
    }

    if(completionAction == SaveAction::OPEN_FLODER){
        qDebug()<<"打开文件位置";
        QFileInfo fileInfo(completeSavePath);
        QString folderPath = fileInfo.absolutePath();
        QProcess::startDetached("xdg-open", QStringList() << folderPath);
        return ;
    }
}

void ScreencapSaver::sendMessage(QString text)
{
    SystemNotification().sendMessage(text, "");
}
